package cn.iocoder.yudao.module.crossborder.order.service.orderaddress;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.order.controller.admin.orderaddress.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.orderaddress.OrderAddressDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.order.dal.mysql.orderaddress.OrderAddressMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.order.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.crossborder.enums.CrossBorderConstants.*;
import cn.iocoder.yudao.module.crossborder.order.enums.AddressTypeEnum;

/**
 * 跨境电商订单地址 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderAddressServiceImpl implements OrderAddressService {

    @Resource
    private OrderAddressMapper orderAddressMapper;

    @Override
    public Long createOrderAddress(OrderAddressSaveReqVO createReqVO) {
        // 插入
        OrderAddressDO orderAddress = BeanUtils.toBean(createReqVO, OrderAddressDO.class);
        orderAddressMapper.insert(orderAddress);

        // 返回
        return orderAddress.getId();
    }

    @Override
    public void updateOrderAddress(OrderAddressSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderAddressExists(updateReqVO.getId());
        // 更新
        OrderAddressDO updateObj = BeanUtils.toBean(updateReqVO, OrderAddressDO.class);
        orderAddressMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderAddress(Long id) {
        // 校验存在
        validateOrderAddressExists(id);
        // 删除
        orderAddressMapper.deleteById(id);
    }

    @Override
        public void deleteOrderAddressListByIds(List<Long> ids) {
        // 删除
        orderAddressMapper.deleteByIds(ids);
        }


    private void validateOrderAddressExists(Long id) {
        if (orderAddressMapper.selectById(id) == null) {
            throw exception(ORDER_ADDRESS_NOT_EXISTS);
        }
    }

    @Override
    public OrderAddressDO getOrderAddress(Long id) {
        return orderAddressMapper.selectById(id);
    }

    @Override
    public PageResult<OrderAddressDO> getOrderAddressPage(OrderAddressPageReqVO pageReqVO) {
        return orderAddressMapper.selectPage(pageReqVO);
    }

}